<template>
	<div class="app-container">
		<el-form :model="data.info" :rules="data.rules" :label-width="language == 'en' ? '150px' : '120px'" class="w-1/2"
			ref="ruleFormRef">

			<el-form-item :label="$t('system.wechat.id') + ':'" prop="gh_id">
				<el-input v-model="data.info.gh_id" :placeholder="$t('message.pleaseEnter')" />
			</el-form-item>

			<el-form-item :label="$t('system.wechat.name') + ':'" prop="wx_name">
				<el-input v-model="data.info.wx_name" :placeholder="$t('message.pleaseEnter')" />
			</el-form-item>

			<el-form-item label="APP_ID:" prop="app_id">
				<el-input v-model="data.info.app_id" :placeholder="$t('message.pleaseEnter')" />
			</el-form-item>

			<el-form-item :label="$t('strategy.payee.secretKey') + 'SECRET:'" prop="secret">
				<el-input v-model="data.info.secret" :placeholder="$t('message.pleaseEnter')" />
			</el-form-item>

			<el-form-item :label="$t('system.wechat.token') + ':'" prop="token">
				<el-input v-model="data.info.token" :placeholder="$t('message.pleaseEnter')" />
			</el-form-item>

			<el-form-item :label="$t('system.wechat.key') + ':'" prop="aes_key">
				<el-input v-model="data.info.aes_key" :placeholder="$t('message.pleaseEnter')" />
			</el-form-item>

			<el-form-item :label="$t('system.wechat.domainFile') + ':'" prop="wx_txt">
				<el-input v-model="data.info.wx_txt" :placeholder="$t('message.pleaseEnter')" readonly>
					<template #append>
						<Upload :list="{ 'data': 'wxtext', 'list': false }" :type="99" :uptype="2"
							@exposeData="expose" />
					</template>
				</el-input>
			</el-form-item>

			<el-form-item :label="$t('strategy.payee.state') + ':'" prop="status">
				<el-select v-model="data.info.status" :placeholder="$t('message.pleaseSelect')">
					<el-option :label="$t('public.enable')" :value="1" />
					<el-option :label="$t('public.disable')" :value="2" />
				</el-select>
			</el-form-item>

			<div class="w-full text-center">
				<div class="inline-block">
					<el-button type="primary" @click="submit(ruleFormRef)">{{ $t('public.save') }}</el-button>
					<el-button @click="cancell">{{ $t('public.cancel') }}</el-button>
				</div>
			</div>

		</el-form>
	</div>
</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n'
const { t: $t } = useI18n()
import Upload from '@/components/Upload/index.vue';
import { upwxdate } from '@/api/system';
import { useRouter, useRoute } from "vue-router";
import { onMounted } from 'vue'
let language = ref()
const { locale } = useI18n()
onMounted(() => {
	language.value = locale.value
})
let ruleFormRef = ref<any>();
const router = useRouter()
const route = useRoute()

let data = reactive({
	info: { // 主体信息
		id: '',
		gh_id: '',
		wx_name: '',
		app_id: '',
		secret: '',
		token: '',
		aes_key: '',
		wx_txt: '',
		status: '',
		ao_id: '',
	},
	rules: { // 表单验证
		app_id: { required: true, message: $t('message.pleaseEnter'), trigger: 'blur' },
		secret: { required: true, message: $t('message.pleaseEnter'), trigger: 'blur' },
		token: { required: true, message: $t('message.pleaseEnter'), trigger: 'blur' },
		aes_key: { required: true, message: $t('message.pleaseEnter'), trigger: 'blur' },
		wx_txt: { required: true, message: $t('message.pleaseEnter'), trigger: 'blur' },
	}
})
// 取消
let cancell = () => {
	getinfo()
	router.back()
}
// 获取信息
let getinfo = () => {
	let info = JSON.parse(decodeURI(route.query.l)).info
	for (let key in data.info) {
		data.info[key] = info[key]
	}
}

// 保存
let submit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid, fields) => {
		if (valid) {
			upwxdate(data.info).then(res => {
				if (res.state == 200) {
					ElMessage({
						message: $t('message.modifySuccess'),
						type: 'success'
					});
					router.back()
					return;
				}
				ElMessage({
					message: $t('message.networkFailed'),
					type: 'error'
				});
			})
		}
	})
}

// 上传返回
let expose = (e) => {
	if (e.status == 200) {
		data.info.wx_txt = e.file.response.data
	} else {
		data.info.wx_txt = ''
	}
}


getinfo()

</script>